/**
 * Copyright (c) 2009-2011, chunquedong(YangJiandong)
 * 
 * This file is part of ChunMap project
 * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE(Version >=3)
 * 
 * History:
 *     2010-05-05  Jed Young  Creation
 */
package chunmap.util;

import java.util.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;

/**
 * 日志记录器
 * 
 * @author chunquedong
 * 
 */
public class Logger {
	private final String name;
	private static int AllLevel = 1;
	private int level;
	
	public Logger(int level, String name) {
		this.level = level;
		this.name = name;
	}

	public boolean isDubug(){
		if(level<=Debug)return true;
		else return false;
	}
	
	public static final int All = 0;
	public static final int Debug = 1;
	public static final int Info = 2;
	public static final int Warn = 3;
	public static final int Error = 4;
	public static final int Fatal = 5;
	public static final int Off = 6;

	public void log(int level, String message) {
		//assert log4debug(level, message);
		log4debug(level, message);
	}

	private boolean log4debug(int level, String message) {
		if (Logger.AllLevel <= level && this.level <= level) {
			System.out.println(getCurrentTime() + "\t" + message + "\t-from "
					+ name);
		}
		return true;
	}
	
	public void debug(String message){
		log(Debug,message);
	}

	private static String getCurrentTime() {
		Date currentTime = new Date();
		DateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String dateString = formatter.format(currentTime);
		return dateString;
	}

	public static void setAllLevel(int level) {
		Logger.AllLevel = level;
	}
}